import {getNoticeInfoList} from '../../../api/index'
Component({
  data:{
    title:"",
    noticeType:"",
    show: false,
    actions: [
      {
        name:"今日"
      },
      {
        name: '近一周',
      },
      {
        name: '近一月',
      },
      {
        name: '近三月',
      },
      {
        name: '近一年',
      },
    ],
    messageTabList:[{
      id:'0',
      label:"全部"
    },{
      id:"1",
      label:"通知 "
    },{
      id:"2",
      label:"提醒"
    }],
    notifyList:[],//消息通知列表
    noticeTitle:"今日"//消息种类
  },
  methods:{
    onLoad(data:any){
      // (course 课程 leave请假 service服务
      type enmu = {course:"课程",leave:"请假",service:"服务"}
      const enmuObj = {course:"课程",leave:"请假",service:"服务"}
      const noticeType:keyof enmu = data.type 
      const title = enmuObj[noticeType]
      this.setData({
        title,
        noticeType
      })
     this.reqNoticeList()
    },
    // 获取信息列表
    async reqNoticeList(){
    const type = this.data.noticeType
    const userId = getApp().getUserInfo().userId

    const {code,data:res} = await getNoticeInfoList({
      type,
      userId
    })
    if(code === 200){
      if(res && res.length > 0){
        this.setData({
          notifyList:res
        })
      }

      
    }
    },
    // 切换tabs
    handleChange(e:any){
      const type = e.detail
      const postData = this.data.postData as any
      if(type === '0'){
        delete postData.type
      }else{
        postData.type = type
      }
      this.reqNoticeList()
    },
    filterOption(){
      this.setData({
        show:true
      })
    },
    // 筛选条件
    onSelect(e:any){
      type TimeFrameType  = "近一周" | "近一月" | "近三月" | "近一年";
    const enmu:Record<TimeFrameType,string> = {
      "近一周":"last_week",
      "近一月":"last_month",
      "近三月":"last_thr_month",
      "近一年":"last_year"
    }
     const type = e.detail.name as TimeFrameType
     this.setData({
      noticeTitle:type
     })
     const timeFrame = enmu[type] 
     const postData = this.data.postData as any
     if(timeFrame){
      postData.timeFrame = timeFrame
     }else{
     delete postData.timeFrame
     }
     this.reqNoticeList()
    },
    onClose(){
      this.setData({
        show:false
      })
    }
  }
})